﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Collections;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore;


namespace STHPMS1.BLL.B_Bus
{
    public class Dp_Preven_PlanBus : BllBase, IDp_Preven_PlanBill
    {
        public void Save<T>(T entity)
            where T : class ,IDp_Preven_Plan
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDp_Preven_Plan entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDp_Preven_Plan
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }

        /// <summary>
        /// 设置最佳年份
        /// </summary>
        /// <param name="road"></param>
        public void SetYear(string anniname, string analy_year)
        {
            Expression<Func<Dp_Preven_Detail, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
            {
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year.ToString() && pre.Secon_Measure == 1 && pre.Preven_Measure == 1;
            }
            else
            {
                exp = pre => pre.Secon_Measure == 1 && pre.Preven_Measure == 1;
            }
            List<Dp_Preven_Detail> dp_list = this.Scene.Bll.Dp_Preven_Detail.Get<Dp_Preven_Detail>(exp, null).ToList<Dp_Preven_Detail>();

            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;

            foreach (Dp_Preven_Detail info in dp_list)
            {
                string year = GetYear(anniname, info.Rdwayid, info.Mainsegid);
                //dao.Update<Dp_Preven_Detail>(new { Main_Year = year }, p => p.Rdwayid == info.Rdwayid && p.Mainsegid == info.Mainsegid);

                dao.Update<Dp_Preven_Detail>(new { Main_Year = year }, p => p.Id==info.Id); //更新养护年份

                //更新规划年度养护需求分析表：方案ID，方案名称
                //dao.Update<Dn_Divi_Planning>(new { Project_Id = info.Id, Project_Name = info.Meas_Ini, }, p => p.RoadnetId == roadnetid && p.Divi_Time == analy_year && p.Mainsegid == info.Mainsegid);

                dao.Update<Dn_Divi_Planning>(new { Segid_Main = 12001 }, p => p.Anni_Name == anniname && p.Divi_Time == analy_year && p.Mainsegid == info.Mainsegid);

                ////更新路网决策路段表
                //if (analy_year == year)  //规划年度和养护年份相同
                //{
                //    //方案ID，方案名称，方案资金，养护年份
                //    dao.Update<Dn_Divi_Planning>(new { Project_Id = info.Id, Project_Name = info.Meas_Ini, Cost = info.Cost, Main_Year = year }, p => p.Anni_Name == anniname && p.Divi_Time == analy_year && p.Mainsegid == info.Mainsegid);
                //}
                //else  
                //{
                //    //之前所有年度为日常养护
                //    int beginYear = Convert.ToInt32(analy_year);
                //    int endYear = Convert.ToInt32(year);
                //    for (int i = beginYear; i < endYear; i++)
                //    {
                //        //养护类型，养护年份
                //        dao.Update<Dn_Divi_Planning>(new { Segid_Main = 12000, Main_Year = i.ToString() }, p => p.Anni_Name == anniname && p.Divi_Time == i.ToString() && p.Mainsegid == info.Mainsegid);
                //    }

                //    //设定养护年度为预防性养护    养护类型，方案ID，方案名称，方案资金，养护年份
                //    dao.Update<Dn_Divi_Planning>(new { Segid_Main = 12001, Project_Id = info.Id, Project_Name = info.Meas_Ini, Cost = info.Cost, Main_Year = year }, p => p.Anni_Name == anniname && p.Divi_Time == analy_year && p.Mainsegid == info.Mainsegid);
                //}

            }
        }

        /// <summary>
        /// 选取效益费用最优的养护年份
        /// </summary>
        /// <param name="rdwayid"></param>
        /// <param name="mainsegid"></param>
        /// <returns></returns>
        public string GetYear(string anniname, string rdwayid, string mainsegid)
        {
            return this.Scene.Dal.Sthpms1.Dp_Preven_Plan.GetYear(anniname, rdwayid, mainsegid);
        }
    }
}
